package xq.gwt.mvc.model;

public class IntegerArrayPropertyModel extends AbstractPropertyModel {
		
	private static final long serialVersionUID = 1665881571892010190L;
	Integer[] value;
	private String separator = ";";

	@Override
	public Object getObjectValue() {		
		return getValue();
	}

	@SuppressWarnings("rawtypes")
	@Override
	public Class getPropertyType() {		
		return Integer[].class;
	}

	
	
	public Integer[] getValue() {
		return value;
	}

	public void setValue(Integer[] value) {
		Integer[] oldValue = this.value;
		this.value = value;
		notifyPropertyChanged(oldValue, this.value);
	}

	@Override
	public String getText() {
		if(getValue() == null)
			return null;
		
		if(getValue().length == 0)
			return "";
				
		String text = "";		
		for (int i = 0; i < getValue().length; i++) {
			text += getValue()[i] + separator;
		}		
		text = text.substring(0,text.lastIndexOf(separator));
		return text;
	}

	@Override
	public void setText(String text) throws ConversionException {
		if(text == null){
			setValue(null);
			return;
		}
		String[] stringVals = text.split(separator);
		Integer[] val = new Integer[stringVals.length];
		for (int i = 0; i < val.length; i++) {
			val[i] = Integer.parseInt(stringVals[i]);
		}
				
		setValue(val);
	}

	public String getSeparator() {
		return separator;
	}

	public void setSeparator(String separator) {
		this.separator = separator;
	}
	
	
	
}
